﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using System.Data.OleDb;
using System.Data.SqlClient ;
namespace ESOL.WinUI.Pur
{
    public partial class frmManageSupplier : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstSupplierEntity mSelectedSupplier;
        #endregion

        #region Constructor
        public frmManageSupplier()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSUPPLIER_TYPE, "SUPPT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPAYMENT_TYPE, "PMTT", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND S.SUPPLIER_ID = '" + supplier.SupplierId + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "S.SUPPLIER_CODE";
            string startDate = "";
            string endDate = "";
            if (dtpBeginDate.EditValue != null)
            {
                startDate = ((DateTime)dtpBeginDate.EditValue).ToString("yyyy-MM-dd");
            }
            if (dtpEndDate.EditValue != null)
            {
                endDate = ((DateTime)dtpEndDate.EditValue).ToString("yyyy-MM-dd");
            }
            DataTable dt = PurPurchaseOrderBs.GetPurchaseOrderOverview(condition, startDate, endDate, orderBy, rowCount);
            gvMain.DataSource = dt;
            ResetGridColor();
        }

        private void ResetGridColor()
        {
            //foreach (DataGridViewRow row in gvMain.Rows)
            //{
            //    if (row.Cells["colIS_EXPIRED"].Value != null && row.Cells["colIS_EXPIRED"].Value.ToString() == "1")
            //    {
            //        row.DefaultCellStyle.ForeColor = Color.Red;
            //        row.DefaultCellStyle.SelectionForeColor = Color.Red;
            //    }
            //    else
            //    {
            //        row.DefaultCellStyle.ForeColor = Color.Black;
            //        row.DefaultCellStyle.SelectionForeColor = Color.Black;
            //    }
            //}
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            ResetGridColor();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSupplier frm = new frmMaintainSupplier();
            frm.ThisActionType = ActionType.View;
            frm.SelectedSupplier = mSelectedSupplier;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中供应商？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedSupplier.Status = "STATUS05";
                bool successful = MstSupplierBs.UpdateMstSupplier(mSelectedSupplier);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSupplier frm = new frmMaintainSupplier();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedSupplier = mSelectedSupplier;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainSupplier frm = new frmMaintainSupplier();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedSupplier.Status != "STATUS02")
            {
                MessageBox.Show("该供应商当前状态不可审核！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSupplier frm = new frmMaintainSupplier();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedSupplier = mSelectedSupplier;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedSupplier.Status != "STATUS01")
            {
                MessageBox.Show("该供应商当前状态不可送审！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中供应商？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedSupplier.Status = "STATUS02";
                bool successful = MstSupplierBs.UpdateMstSupplier(mSelectedSupplier);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            btnPurchaseDetail.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedSupplier = MstSupplierBs.GetMstSupplierById(CommonFunctions.ToString(dr["SUPPLIER_ID"]));
            if (mSelectedSupplier.Status == "STATUS01")
            {
                btnSubmit.Enabled = true;
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedSupplier.Status == "STATUS02")
            {
                btnApprove.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedSupplier.Status == "STATUS03")
            {
                btnPurchaseDetail.Enabled = true;
                btnDelete.Enabled = true;
                btnModify.Enabled = true;
            }
            else if (mSelectedSupplier.Status == "STATUS04")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedSupplier.Status == "STATUS05")
            {
            }
        }

        private void btnPurchaseDetail_Click(object sender, EventArgs e)
        {
            frmManageOverViewDetail frm = new frmManageOverViewDetail(mSelectedSupplier);
            frm.MdiParent = this.ParentForm;
            frm.Show();
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }
        #endregion
        private DataSet xsldata(string filepath)
        {
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
            string strCom = "SELECT * FROM [Sheet1$]";
            Conn.Open();
            System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
            DataSet ds = new DataSet();

            myCommand.Fill(ds, "[Sheet1$]");
            BindDataGrid();
            return ds;
        }
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (open.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(open.FileName);
            }

           
        }

    }
}
